
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/ImageHelper.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/RouteHelper.dart';
import '../AppMain.dart';


class LaunchApp extends StatelessWidget {

  static bool isStart=false;

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(create: (context){
       return _Counter();
    },child: Launch(),);
  }
}

class Launch extends StatefulWidget {

  @override
  _LaunchAppState createState() => _LaunchAppState();

}

class _LaunchAppState extends State<Launch> {

  var timer;

  @override
  void initState() {
    super.initState();
    var _counter = Provider.of<_Counter>(context,listen: false);
    timer=Timer.periodic(Duration(seconds: 1), (timer) {
      _counter.minuNum();
      if(_counter._num<=0){
        stop();
      }
    });
  }

  @override
  void dispose() {
    timer?.cancel();
    super.dispose();
  }

  void stop(){
    timer.cancel();
    if(!LaunchApp.isStart){
      LaunchApp.isStart=true;
      Navigator.pop(context);
      toAppMain(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      child: Stack(
        children: [
          Image.asset(assetPath("img_bg_splash.png"),fit:BoxFit.fill,
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,),
          Container(
            padding: EdgeInsets.fromLTRB(0, MediaQuery.of(context).padding.top+10,12, 0),
            alignment: Alignment.topRight,
            child: Selector(builder: ( context, value,child){
              return SizedBox(
                height: 32,
                child: OutlinedButton(onPressed: (){
                  stop();
                },
                  child: Text("跳过$value"),
                  style: ButtonStyle(padding: MaterialStateProperty.all(EdgeInsets.fromLTRB(1, 0, 1, 0),),
                      fixedSize: MaterialStateProperty.all(Size(40, 3))),
                ),
              );
            }, selector: (_,_Counter v){
              return v._num;
            }),
          ),
        ],
      ),
    );
  }
}

class _Counter with ChangeNotifier {
  static int macSize=4;
  int _num = macSize;

  void minuNum() {
    _num--;
    notifyListeners();
  }
}


